﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.TeamFoundation.Client
Imports System.Collections.ObjectModel

Public Class TFSImport

    Private m_ItemWrapperCollection As ItemCollection

    Public Sub Import(ByRef ItemWrapperCollection As ItemCollection, ByVal TreeType As Node.TreeType)
        ' Clear current content.
        ItemWrapperCollection.Clear()
        Try
            ' Store localy for ease of access
            m_ItemWrapperCollection = ItemWrapperCollection
            ' Do import
            Dim NodeCol As NodeCollection = Nothing
            Select Case TreeType
                Case Node.TreeType.Area
                    NodeCol = Application.SelectedProject.AreaRootNodes
                Case Node.TreeType.Iteration
                    NodeCol = Application.SelectedProject.IterationRootNodes
            End Select

            Dim ProjectItem As ProjectItem = New ProjectItem(Application.SelectedProject)
            m_ItemWrapperCollection.Add(ProjectItem)

            ImportNodes(ProjectItem, NodeCol)

            ImportWorkItems(ProjectItem)

            ' Item added to the collection as the primary-Item.
            If m_ItemWrapperCollection.Count > 0 Then
                m_ItemWrapperCollection.Current = ProjectItem
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ImportNodes(ByVal Parent As ProjectItem, ByRef Nodes As NodeCollection)
        ' Add parent Nodes
        For Each n As Node In Nodes
            Dim newNodeItem As NodeItem = New NodeItem(n)
            m_ItemWrapperCollection.Add(newNodeItem)
            ImportChildNodes(n, newNodeItem)
        Next

        ' Add relationships
        For Each tn As Node In Nodes
            Dim Item As Item = m_ItemWrapperCollection.Find(CStr(tn.Uri.ToString))
            ' Add Top level nodes as children
            m_ItemWrapperCollection.AddChild(Parent, Item)
            For Each n As Node In Nodes
                If Not n Is tn Then

                    Dim Sibling As Item = m_ItemWrapperCollection.Find(CStr(n.Uri.ToString))
                    ' Add top level nodes as siblings
                    If Item Is Nothing OrElse Sibling Is Nothing Then
                        Throw New Exception("Could not find node")
                    End If
                    m_ItemWrapperCollection.AddSibling(Item, Sibling)
                End If
            Next
        Next

    End Sub

    Private Sub ImportChildNodes(ByRef Node As Node, ByVal iw As Item)
        For Each n As Node In Node.ChildNodes
            Dim newNodeItem As New NodeItem(n)
            m_ItemWrapperCollection.Add(newNodeItem)
            m_ItemWrapperCollection.AddChild(iw, newNodeItem)
            ImportChildNodes(n, newNodeItem)
        Next
    End Sub

    Private Sub ImportWorkItems(ByVal ProjectItem As ProjectItem)
        If Not Application.SelectedStoredQuery Is Nothing Then
            Dim strt As Long = Now.Ticks
            Dim QueryText As String = Application.SelectedStoredQuery.QueryText.Replace("@project", String.Format("'{0}'", ProjectItem.Value.Name))
            Dim wis As WorkItemCollection = Application.SelectedProject.Store.Query(QueryText)
            For Each WI As WorkItem In wis
                Dim newWIItem As WIItem = New WIItem(WI)
                m_ItemWrapperCollection.Add(newWIItem)

                ' Add realtionships
                Dim NodeItem As Item = ProjectItem
                Select Case Application.DisplayBy
                    Case Node.TreeType.Area
                        If Not WI.AreaPath = ProjectItem.Name Then
                            Dim Path As String = WI.AreaPath.Substring(Application.SelectedProject.Name.Length, WI.AreaPath.Length - Application.SelectedProject.Name.Length)
                            Dim node = Application.SelectedProject.FindNodeInSubTree(Path, Microsoft.TeamFoundation.WorkItemTracking.Client.Node.TreeType.Area)
                            NodeItem = m_ItemWrapperCollection.Find(node.Uri.ToString)
                        End If
                    Case Node.TreeType.Iteration
                        If Not WI.AreaPath = ProjectItem.Name Then
                            Dim Path As String = WI.AreaPath.Substring(Application.SelectedProject.Name.Length, WI.IterationPath.Length - Application.SelectedProject.Name.Length)
                            Dim node = Application.SelectedProject.FindNodeInSubTree(Path, Microsoft.TeamFoundation.WorkItemTracking.Client.Node.TreeType.Iteration)
                            NodeItem = m_ItemWrapperCollection.Find(node.Uri.ToString)
                        End If
                End Select
                m_ItemWrapperCollection.AddWorkItem(NodeItem, newWIItem)
                ' TODO: Add Inter Work Item Relationships
                Debug.Print("ImportWorkItems took {0} seconds", New TimeSpan(Now.Ticks - strt).TotalSeconds)
            Next
        End If
    
    End Sub

End Class
